home *** CD-ROM | disk | FTP | other *** search
/ PC Plus SuperCD (UK) 1998 August / PC Plus SuperCD 50b Issue 142 (CD142b) (August 1998).iso / essent / FIXES / CSeries.exe / issue100 / CPROG16.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  1014 b   |  35 lines

  1. /* CPROG16.CPP - types itself to the screen as a demo of reading a file a
  2.          character at a time */
  3.  
  4. #include <stdio.h>
  5. #include <conio.h>
  6. #include <stdlib.h>
  7.  
  8. void main(void)
  9. {
  10. FILE *file1;    // Declares a file pointer
  11. char c;        // A byte of storage to receive each incoming character
  12.  
  13.     if( ! ( file1=fopen("cprog16.cpp", "r") ) )
  14.         {
  15.         cputs("Couldn't open CPROG12.CPP\n");
  16.         exit(0);
  17.         }
  18.  
  19.     /* Tries to open CPROG16.CPP for reading. File1 gets the return value
  20.     from fopen() - 0 for failure, or a file pointer. */
  21.  
  22.     while( (c=fgetc(file1)) != EOF )
  23.         putchar( c );
  24.  
  25.     /* The loop runs as long as the return value from fgetc() is not EOF.
  26.     EOF means end of file - it is a particular value which has been
  27.     #defined as EOF. Note how the file's identifying number, stored in
  28.     file1, is passed to fgetc() to tell it where you want it to take the
  29.     next character from. */
  30.  
  31.     fclose(file1);    // After closing the file, file1 is available for
  32.             // resue with another file.
  33.     putchar('\n');
  34. }
  35.